package com.htht.smartschool.util.system;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import com.htht.smartschool.model.exam.ExamResultEnum;
import com.htht.smartschool.model.exam.ExamResultEnum.ResultEnum;

public class MapUtil { 
   
    /** 
     * 将一个 Map 对象转化为一个 JavaBean 
     */ 
    @SuppressWarnings("rawtypes")
	public static Object convertMap(Class type, Map map){
    	Object obj = null;
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(type);
			obj = type.newInstance(); 
	        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors(); 
	        for (int i = 0; i< propertyDescriptors.length; i++) { 
	            PropertyDescriptor descriptor = propertyDescriptors[i]; 
	            String propertyName = descriptor.getName(); 
	            if (map.containsKey(propertyName)) { 
	                Object value = map.get(propertyName); 
	                Object[] args = new Object[1]; 
	                args[0] = value;
	                if(getObjecPrivate(obj,propertyName)){
	                	 try {
							descriptor.getWriteMethod().invoke(obj, args);
						} catch (Exception e) {
							try {
								args[0] = Long.valueOf(String.valueOf(value));
								descriptor.getWriteMethod().invoke(obj, args);
							} catch (Exception e1) {
								try {
									args[0] = (int)Integer.parseInt(String.valueOf(value));
									descriptor.getWriteMethod().invoke(obj, args);
								} catch (InvocationTargetException e2) {
									e2.printStackTrace();
								}
							}
						} 
	                }
	            } 
	        } 
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}catch (SecurityException e) {
			e.printStackTrace();
		} 
        return obj; 
    } 
    public static boolean getObjecPrivate(Object obj,String propertyName){
    	try {
			obj.getClass().getDeclaredField(propertyName);
			return true;
		} catch (NoSuchFieldException e) {
			return false;
		} catch (SecurityException e) {
			return false;
		}
    }
    
    /** 
     * 将一个 JavaBean 对象转化为一个  Map 
     */ 
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map convertBean(Object bean){ 
    	Map returnMap = new HashMap(); 
		try {
			Class type = bean.getClass();
			BeanInfo beanInfo = Introspector.getBeanInfo(type);
			PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors(); 
		    for (int i = 0; i< propertyDescriptors.length; i++) { 
		        PropertyDescriptor descriptor = propertyDescriptors[i]; 
		        String propertyName = descriptor.getName(); 
		        if (!propertyName.equals("class")) { 
		            Method readMethod = descriptor.getReadMethod(); 
		            Object result = readMethod.invoke(bean, new Object[0]); 
		            if (result != null) { 
		                returnMap.put(propertyName, result); 
		            } else { 
		                returnMap.put(propertyName, ""); 
		            } 
		        } 
		    } 
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} 
        return returnMap; 
    } 
}